﻿namespace LeadMaster.Controllers
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Entity;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    using LeadMaster.Models;
    using LeadMaster.DAL;
    using LeadMaster.ViewModels;

    public class ContactsController : Controller
    {
        private DunsmoreContext db = new DunsmoreContext();

        public ActionResult Index()
        {
            ViewModelContacts viewModel = new ViewModelContacts();
            viewModel.Contacts = db.Contacts.OrderBy(m => m.Surname).ThenBy(m => m.Forename).ToList();
            return PartialView(viewModel); 
        }

        public ActionResult PanelIndex(ViewModelContacts viewModel)
        {
            return PartialView("Index",viewModel);
        }

        public ActionResult Details(int id = 0)
        {
            Contacts contacts = db.Contacts.Find(id);
            if (contacts == null)
            {
                return HttpNotFound();
            }
            return View(contacts);
        }

        public ActionResult Create()
        {
            ViewModelContacts viewModel = new ViewModelContacts();
            viewModel.Contact = new Contacts();
            viewModel.Contacts = db.Contacts.ToList();
            viewModel.Companies =  db.Companies.ToList()
                                   .Select(c => new SelectListItem
                                        {
                                            Value = c.CompanyID.ToString(),
                                            Text = c.Company
                                        });
            return PartialView("Index",viewModel);
        }

        [HttpPost]
        public ActionResult Create(ViewModelContacts viewModel)
        {
            if (ModelState.IsValid)
            {
                db.Contacts.Add(viewModel.Contact);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(viewModel);
        }

        public ActionResult CreateWithCompany(int id=0)
        {
            ViewModelContacts viewModel = new ViewModelContacts();
            viewModel.Company = db.Companies.Find(id);
            viewModel.Contact = new Contacts();
            viewModel.Contact.CompanyID = id;
            return PartialView("CreateWithCompany", viewModel);
        }

        public ActionResult CreateWithCompanyPanel(int id = 0)
        {
            ViewModelContacts viewModel = new ViewModelContacts();
            viewModel.Company = db.Companies.Find(id);
            viewModel.Contact = new Contacts();
            viewModel.Contact.CompanyID = id;
            viewModel.PanelView = true;
            return PartialView("CreateWithCompany", viewModel);
        }

        [HttpPost]
        public ActionResult CreateWithCompany(ViewModelContacts viewModel)
        {
            if (ModelState.IsValid)
            {
                db.Contacts.Add(viewModel.Contact);
                db.SaveChanges();
            }

            viewModel.Company = db.Companies.Find(viewModel.Contact.CompanyID);
            viewModel.Contacts = db.Contacts.Where(m => m.CompanyID == viewModel.Company.CompanyID).OrderBy(m => m.Surname).ThenBy(m => m.Forename);
            if (viewModel.Company == null)
            {
                return HttpNotFound();
            }

            return PartialView("Index", viewModel);
        }

        public ActionResult Edit(int id = 0)
        {
            Contacts contacts = db.Contacts.Find(id);
            if (contacts == null)
            {
                return HttpNotFound();
            }
            return PartialView(contacts);
        }

        [HttpPost]
        public ActionResult Edit(Contacts contacts)
        {
            if (ModelState.IsValid)
            {
                db.Entry(contacts).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index", "Home");
            }
            return RedirectToAction("Index", "Home");
        }

        public JsonResult Delete(int id = 0)
        {
            Contacts contact = db.Contacts.Find(id);
            if (contact != null)
            {
                db.Contacts.Remove(contact);
                db.SaveChanges();
            }

            return Json(id, JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        public ActionResult DeleteConfirmed(int id)
        {
            Contacts contacts = db.Contacts.Find(id);
            db.Contacts.Remove(contacts);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}